package edu.whut.mall.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import edu.whut.mall.admin.domain.RoleParams;
import edu.whut.mall.admin.enums.SysStatusEnum;
import edu.whut.mall.admin.service.ISysResourcesService;
import edu.whut.mall.admin.service.ISysRoleService;
import edu.whut.mall.admin.vo.ResourceVo;
import edu.whut.mall.admin.vo.RoleVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.mapper.SysRoleMapper;
import edu.whut.mall.mapper.SysRoleResourcesMapper;
import edu.whut.mall.mapper.SysUserRoleMapper;
import edu.whut.mall.model.SysRole;
import edu.whut.mall.model.SysRoleExample;
import edu.whut.mall.model.SysRoleResourcesExample;
import edu.whut.mall.model.SysUserRoleExample;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: d9lab-mall
 * @description: 角色实现类
 * @author: Wayne
 * @create: 2020-04-17 11:39
 **/
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements ISysRoleService {

    private final SysRoleMapper sysRoleMapper;

    private final ISysResourcesService sysResourcesService;

    private final SysUserRoleMapper sysUserRoleMapper;

    private final SysRoleResourcesMapper sysRoleResourcesMapper;

    @Override
    public Integer insertOneRole(RoleParams roleParams) {
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(roleParams,sysRole);
        sysRole.setAvailable(SysStatusEnum.AVAILABLE.getCode());
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        return sysRoleMapper.insertSelective(sysRole);
    }

    @Override
    public Integer updateRole(RoleParams roleParams) {
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(roleParams,sysRole);
        sysRole.setUpdateTime(new Date());
        return sysRoleMapper.updateByPrimaryKeySelective(sysRole);
    }

    @Override
    public CommonPage<RoleVo> listRoles(Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum, size);
        List<SysRole> roleList=sysRoleMapper.selectByExample(new SysRoleExample());
        List<RoleVo> roleVoList=roleList.stream().map(this::tans2Vo).collect(Collectors.toList());
        return CommonPage.restPage(roleVoList);
    }

    @Override
    public List<ResourceVo> listResourceVosByRoleId(Long roleId) {
          return sysResourcesService.listResourceVosByRoleId(roleId);

    }

    @Override
    @Transactional
    public Integer deleteOneRole(Long roleId) {
        sysRoleMapper.deleteByPrimaryKey(roleId);
        SysRoleResourcesExample roleResourcesExample = new SysRoleResourcesExample();
        roleResourcesExample.createCriteria().andRoleIdEqualTo(roleId);
        sysRoleResourcesMapper.deleteByExample(roleResourcesExample);
        SysUserRoleExample userRoleExample = new SysUserRoleExample();
        userRoleExample.createCriteria().andRoleIdEqualTo(roleId);
        return sysUserRoleMapper.deleteByExample(userRoleExample);

    }

    /**
     * 转换为展示层
     * @param sysRole
     */
    private RoleVo tans2Vo(SysRole sysRole){
        RoleVo roleVo = new RoleVo();
        BeanUtil.copyProperties(sysRole,roleVo);
        return roleVo;
    }


}
